package fourfaith.com.ff_sl21_app;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import fourfaith.com.ff_sl21_app.fragment.AlarmFragment;
import fourfaith.com.ff_sl21_app.fragment.HomeFragment;
import fourfaith.com.ff_sl21_app.fragment.NoticeFragment;
import fourfaith.com.ff_sl21_app.fragment.ReportDetailFragment;
import fourfaith.com.ff_sl21_app.fragment.ReportDeviceFragment;
import fourfaith.com.ff_sl21_app.fragment.UserFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,HomeFragment.OnFragmentInteractionListener,
        AlarmFragment.OnFragmentInteractionListener,UserFragment.OnFragmentInteractionListener,NoticeFragment.OnFragmentInteractionListener,
        ReportDeviceFragment.OnFragmentInteractionListener,ReportDetailFragment.OnFragmentInteractionListener {

    private LinearLayout ly_tab_menu_home,ly_tab_menu_alarm,ly_tab_menu_report,ly_tab_menu_notice,ly_tab_menu_user;
    private TextView tx_menu_name_home,tx_menu_name_alarm,tx_menu_name_report,tx_menu_name_notice,tx_menu_name_user;
    private HomeFragment homeFragment;
    private AlarmFragment alarmFragment;
    private UserFragment userFragment;
    private ReportDeviceFragment reportFragment;
    private NoticeFragment noticeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        ly_tab_menu_home.performClick();
    }

    private void initView(){
        ly_tab_menu_home = (LinearLayout) findViewById(R.id.ly_tab_menu_home);
        ly_tab_menu_alarm = (LinearLayout) findViewById(R.id.ly_tab_menu_alarm);
        ly_tab_menu_report = (LinearLayout) findViewById(R.id.ly_tab_menu_report);
        ly_tab_menu_notice = (LinearLayout) findViewById(R.id.ly_tab_menu_notice);
        ly_tab_menu_user = (LinearLayout) findViewById(R.id.ly_tab_menu_user);

        tx_menu_name_home = (TextView)findViewById(R.id.tx_menu_name_home);
        tx_menu_name_alarm = (TextView)findViewById(R.id.tx_menu_name_alarm);
        tx_menu_name_report = (TextView)findViewById(R.id.tx_menu_name_report);
        tx_menu_name_notice = (TextView)findViewById(R.id.tx_menu_name_notice);
        tx_menu_name_user = (TextView)findViewById(R.id.tx_menu_name_user);

        ly_tab_menu_home.setOnClickListener(this);
        ly_tab_menu_alarm.setOnClickListener(this);
        ly_tab_menu_report.setOnClickListener(this);
        ly_tab_menu_notice.setOnClickListener(this);
        ly_tab_menu_user.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(transaction);
        switch (v.getId()) {
            case R.id.ly_tab_menu_home:
                setSelected();
                tx_menu_name_home.setSelected(true);
                if(homeFragment==null){
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.fragment_container,homeFragment);
                }else{
                    transaction.show(homeFragment);
                }
                break;
            case R.id.ly_tab_menu_alarm:
                setSelected();
                tx_menu_name_alarm.setSelected(true);
                if(alarmFragment==null){
                    alarmFragment = new AlarmFragment();
                    transaction.add(R.id.fragment_container,alarmFragment);
                }else{
                    transaction.show(alarmFragment);
                }
                break;
            case R.id.ly_tab_menu_report:
                setSelected();
                tx_menu_name_report.setSelected(true);
                if(reportFragment==null){
                    reportFragment = new ReportDeviceFragment();
                    transaction.add(R.id.fragment_container,reportFragment);
                }else{
                    transaction.show(reportFragment);
                }
                break;
            case R.id.ly_tab_menu_notice:
                setSelected();
                tx_menu_name_notice.setSelected(true);
                if(noticeFragment==null){
                    noticeFragment = new NoticeFragment();
                    transaction.add(R.id.fragment_container,noticeFragment);
                }else{
                    if(noticeFragment.data==null || noticeFragment.data.size()==0){
                        //空数据、请求错误则重新加载
                        transaction.remove(noticeFragment);
                        noticeFragment = new NoticeFragment();
                        transaction.add(R.id.fragment_container,noticeFragment);
                    }else{
                        transaction.show(noticeFragment);
                    }
                }
                break;
            case R.id.ly_tab_menu_user:
                setSelected();
                tx_menu_name_user.setSelected(true);
                if(userFragment==null){
                    userFragment = new UserFragment();
                    transaction.add(R.id.fragment_container,userFragment);
                }else{
                    transaction.show(userFragment);
                }
                break;
        }
        transaction.commit();
    }
    public void hideAllFragment(FragmentTransaction transaction){
        if(homeFragment!=null){
            transaction.hide(homeFragment);
        }
        if(alarmFragment!=null){
            transaction.hide(alarmFragment);
        }
        if(userFragment!=null){
            transaction.hide(userFragment);
        }
        if(noticeFragment!=null){
            transaction.hide(noticeFragment);
        }
        if(reportFragment!=null){
            transaction.hide(reportFragment);
        }
    }

    //重置所有文本的选中状态
    private void setSelected() {
        tx_menu_name_home.setSelected(false);
        tx_menu_name_alarm.setSelected(false);
        tx_menu_name_report.setSelected(false);
        tx_menu_name_notice.setSelected(false);
        tx_menu_name_user.setSelected(false);
    }
    @Override
    public void onFragmentInteraction(Uri uri) {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //resultCode就是在B页面中返回时传的parama，可以根据需求做相应的处理
        if(resultCode==1)
        {
            finish();
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
            System.exit(0);        }
    }

/*    //屏蔽返回键的代码
    public boolean onKeyDown(int keyCode,KeyEvent event){
        switch(keyCode){
            case KeyEvent.KEYCODE_HOME:return true;
            case KeyEvent.KEYCODE_BACK:return true;
            case KeyEvent.KEYCODE_CALL:return true;
            case KeyEvent.KEYCODE_SYM: return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN: return true;
            case KeyEvent.KEYCODE_VOLUME_UP: return true;
            case KeyEvent.KEYCODE_STAR: return true;
        }
        return super.onKeyDown(keyCode, event);
    }*/

    @Override
    public boolean onKeyDown(int keyCode,KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK)
            return true;//不执行父类点击事件
        return super.onKeyDown(keyCode, event);//继续执行父类其他点击事件
    }

}
